package cn.icebreak.cartoon.ui.read;

import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.ScrollView;
import android.widget.TextView;

import java.util.Arrays;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import cn.icebreak.cartoon.R;
import cn.icebreak.cartoon.global.GlideApp;
import cn.icebreak.cartoon.ui.BaseActivity;

public class ReadActivity extends BaseActivity {

    @BindView(R.id.iv_read_1)
    ImageView ivRead1;
    @BindView(R.id.iv_read_2)
    ImageView ivRead2;
    @BindView(R.id.iv_read_3)
    ImageView ivRead3;
    @BindView(R.id.iv_read_4)
    ImageView ivRead4;
    @BindView(R.id.iv_read_5)
    ImageView ivRead5;
    @BindView(R.id.iv_read_6)
    ImageView ivRead6;
    @BindView(R.id.iv_read_7)
    ImageView ivRead7;
    @BindView(R.id.iv_read_8)
    ImageView ivRead8;


    List<ImageView> imageViews;
    @BindView(R.id.iv_read_list)
    ImageView ivReadList;
    @BindView(R.id.iv_read_left)
    ImageView ivReadLeft;
    @BindView(R.id.tv_read_num)
    TextView tvReadNum;
    @BindView(R.id.iv_read_right)
    ImageView ivReadRight;

    int page = 1;
    @BindView(R.id.scrollView_read)
    ScrollView scrollViewRead;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_read);
        ButterKnife.bind(this);

        imageViews = Arrays.asList(ivRead1, ivRead2, ivRead3, ivRead4, ivRead5, ivRead6, ivRead7, ivRead8);


        initView1();
    }

    void initView1() {

        String[] urls = new String[]{
                "http://7xl23i.com2.z0.glb.qiniucdn.com/o_1a49ff2gui3o1bkv1cu61k5sc3k7.jpg",
                "http://7xl23i.com2.z0.glb.qiniucdn.com/o_1a3slu3nor5i1t181le757386ch.jpg",
                "http://7xl23i.com2.z0.glb.qiniucdn.com/o_1a3slu3no12hkp5j1rog15lehjki.jpg",
                "http://7xl23i.com2.z0.glb.qiniucdn.com/o_1a3slu3norm0hgs108db3v1aipj.jpg",
                "http://7xl23i.com2.z0.glb.qiniucdn.com/o_1a3slu3nsn0h1lk0ig82qf1dukk.jpg",
                "http://7xl23i.com2.z0.glb.qiniucdn.com/o_1a3slu3ntchhkf978e1qac1gf8l.jpg",
                "http://7xl23i.com2.z0.glb.qiniucdn.com/o_1a3slu3ntuuk1hb8aku78j18l5m.jpg",
                "http://7xl23i.com2.z0.glb.qiniucdn.com/o_1a3slu3nte9dbll1tc8vmdn30n.jpg"
        };
        initIvView(urls);
        scrollViewRead.scrollTo(0, 0);
    }

    void initView2() {

        String[] urls = new String[]{
                "http://7xl23i.com2.z0.glb.qiniucdn.com/o_1a49heo5410tf1tff15f61rt7ksa7.jpg",
                "http://7xl23i.com2.z0.glb.qiniucdn.com/o_1a3sm974hond17i0fvtjku1eq5h.jpg",
                "http://7xl23i.com2.z0.glb.qiniucdn.com/o_1a3sm974h103j1n9s5q3jo6i5j.jpg",
                "http://7xl23i.com2.z0.glb.qiniucdn.com/o_1a3sm974i1b66df9gl21d88okuk.jpg",
                "http://7xl23i.com2.z0.glb.qiniucdn.com/o_1a3sm974jnge1sk1ffe1vj1129el.jpg",
                "http://7xl23i.com2.z0.glb.qiniucdn.com/o_1a3sm974j1om21d5h1fk712gg11obm.jpg",
                "http://7xl23i.com2.z0.glb.qiniucdn.com/o_1a3sm974j1tlecbg1o6robj1b4ln.jpg",
                "http://7xl23i.com2.z0.glb.qiniucdn.com/o_1a3sm974f1s391044enbq710jag.jpg"
        };
        initIvView(urls);
        scrollViewRead.scrollTo(0, 0);
    }

    void initView3() {

        String[] urls = new String[]{
                "http://7xl23i.com2.z0.glb.qiniucdn.com/o_1a49hv9ethu0151c8hn1kj7a3v7.jpg",
                "http://7xl23i.com2.z0.glb.qiniucdn.com/o_1a3snnntu3le11pmo2k17tgrt2h.jpg",
                "http://7xl23i.com2.z0.glb.qiniucdn.com/o_1a3snnntupq15vo1a2m1k0dfi2i.jpg",
                "http://7xl23i.com2.z0.glb.qiniucdn.com/o_1a3snnntu1qbs1pfp1naecho1korj.jpg",
                "http://7xl23i.com2.z0.glb.qiniucdn.com/o_1a3snnntviqj1hg4u471tk8lek.jpg",
                "http://7xl23i.com2.z0.glb.qiniucdn.com/o_1a3snnnu018u88ic1gsdqp5uakl.jpg",
                "http://7xl23i.com2.z0.glb.qiniucdn.com/o_1a3snnnu0s22nstnb9a7a5d4m.jpg",
                "http://7xl23i.com2.z0.glb.qiniucdn.com/o_1a3snnnu01a5mj3612olb8tjvjn.jpg"
        };
        initIvView(urls);
        scrollViewRead.scrollTo(0, 0);
    }

    void initIvView(String... urls) {

        int i = 0;
        for (String url : urls
                ) {
            loadIv(imageViews.get(i), urls[i]);
            i++;
        }

    }

    void loadIv(ImageView view, String url) {

        GlideApp
                .with(this)
                .load(url)
                .into(view);

    }

    @OnClick({R.id.iv_read_left, R.id.iv_read_right})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.iv_read_left:
                setPageLeft();

                break;
            case R.id.iv_read_right:
                setPageRight();
                break;
        }
    }

    void setPageLeft() {
        page--;
        page = page < 1 ? 1 : page;

        tvReadNum.setText("" + page);

        if (page == 1){
            return;
        }
        if (page % 3 == 1) {
            initView1();
        } else if (page % 3 == 2) {
            initView2();
        } else {
            initView3();
        }
    }

    void setPageRight() {
        page++;
        page = page < 1 ? 1 : page;
        tvReadNum.setText("" + page);
        if (page % 3 == 1) {
            initView1();
        } else if (page % 3 == 2) {
            initView2();
        } else {
            initView3();
        }
    }
}
